package org.nhommua.common;

import java.util.Iterator;
import java.util.Set;

import org.hibernate.Session;
import org.nhommua.dto.Address;
import org.nhommua.dto.AvatarImages;
import org.nhommua.dto.Bill;
import org.nhommua.dto.BillContent;
import org.nhommua.dto.Product;
import org.nhommua.dto.ProductImages;
import org.nhommua.dto.ProductType;
import org.nhommua.dto.Provider;
import org.nhommua.dto.Users;
import org.nhommua.persistence.HibernateUtil;

public class Main {
	public static void main(String[] args){
			AvatarImages avatarImages1=new AvatarImages();
			AvatarImages avatarImages2=new AvatarImages();
			
		Users users1=new Users();
		users1.setGender(true);
			users1.getAvatarImages().add(avatarImages1);
			users1.getAvatarImages().add(avatarImages2);
			avatarImages1.setUsers(users1);
			avatarImages2.setUsers(users1);
		Users users2=new Users();
		users2.setGender(false);
		
		ProductType productType=new ProductType();
		ProductImages productImages1=new ProductImages();
		ProductImages productImages2=new ProductImages();
		
		Provider provider=new Provider();
		provider.setHotline("019312312");
		provider.setPhoneNumber("423415431523");
		
		
			Address address1=new Address();
			Address address2=new Address();
		provider.getAddress().add(address1);	
		provider.getAddress().add(address2);
		
		Product product1=new Product();
		Product product2=new Product();
		product1.setProvider(provider);
		product2.setProvider(provider);
		product1.setProductType(productType);
		product2.setProductType(productType);
		product1.getProductImages().add(productImages1);
		product1.getProductImages().add(productImages2);
		Bill bill=new Bill();
		
			BillContent billContent1=new BillContent();
			billContent1.setNumberProduct(2);
			billContent1.setTotalMoney("10000");
			billContent1.setProduct(product1);
			
			BillContent billContent2=new BillContent();
			billContent2.setNumberProduct(6);
			billContent2.setTotalMoney("20000");
			billContent2.setProduct(product2);
		
		bill.getBillContents().add(billContent1);
		bill.getBillContents().add(billContent2);
		bill.setUsers(users1);
		users1.getBills().add(bill);
		
		HibernateUtil hibernateUtil=new HibernateUtil();
		Session session=hibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		session.save(users1);
		session.save(users2);
		session.save(productType);
		session.save(provider);
		session.save(product1);
		session.save(product2);
		session.delete(users1);
		session.getTransaction().commit();
		session.close();
		
		
		session=hibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		Bill bill1 =(Bill)session.get(Bill.class,Long.valueOf(1));
		if(bill1!=null){
			System.out.println("BillId :"+bill1.getBillId());
			Set<BillContent> billContents=bill1.getBillContents();
			Iterator<BillContent> iterator=billContents.iterator();
			BillContent billContent=new BillContent();
			while(iterator.hasNext()){
				System.out.println("-------------------");
				billContent=iterator.next();
				System.out.println("numberProduct :"+billContent.getNumberProduct());
				System.out.println("totalMoney :"+billContent.getTotalMoney());
				System.out.println("productId :"+billContent.getProduct().getProductId());
			}
		}
		session.close();
	}
}
